package com.orcs.leefei.assistant.presenter.component;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.View;

import com.larvalabs.svgandroid.SVGParser;
import com.orcs.leefei.assistant.utils.MeasureUtils;

/**
 * Created by Administrator on 2017/4/10.
 */

public abstract class WeatherView extends View {
    public static int COLOR_CLOUD = Color.parseColor("#FFFFFF");
    public static int COLOR_CLOUD_LIGHT = Color.parseColor("#d0d9ff");
    public static int COLOR_NIGHT_CLOUD_LIGHT = Color.parseColor("#0277bd");
    public static int COLOR_STAR = Color.parseColor("#F2BB35");
    public static int COLOR_SUN = Color.parseColor("#FAE2AA");
    public static int COLOR_LIGHTNING = Color.parseColor("#F2BB35");

    public static float SCALE_DEFAULT = 0.57F;
    public static int DELAY = 40;

    private int defaultSize;
    protected Paint paint;
    protected int viewWidth;
    protected int viewHeight;
    protected int viewCenterX;
    protected int viewCenterY;
    protected RectF pathBounds;
    protected Matrix scaleMatrix;

    @Override
    public boolean removeCallbacks(Runnable action) {
        return super.removeCallbacks(action);
    }

    public WeatherView(Context context) {
        this(context, null);
    }

    public WeatherView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    protected void init(Context context) {
        defaultSize = MeasureUtils.dpTopx(context, 120);
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        pathBounds = new RectF();
        scaleMatrix = new Matrix();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int size = Math.min(MeasureUtils.resolveSize(defaultSize, widthMeasureSpec),
                MeasureUtils.resolveSize(defaultSize, heightMeasureSpec));
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w != oldw || h != oldh) {
            viewWidth = getMeasuredWidth();
            viewHeight = getMeasuredHeight();
            viewCenterX = viewWidth / 2;
            viewCenterY = viewHeight / 2;
            onGetViewSize();
        }
    }

    protected void onGetViewSize() {

    }

    protected int getLightCloudColor() {
        return COLOR_CLOUD_LIGHT;
    }

    protected float getScale(RectF bounds) {
        return Math.min(viewWidth / bounds.width(), viewHeight / bounds.height());
    }

    protected Path createPath(@StringRes int resId) {
        return SVGParser.parsePath(getContext().getString(resId));
    }

    protected void adjustToCenter(Canvas canvas, Path path) {
        computeBounds(path);
        canvas.translate(viewCenterX - pathBounds.centerX(), viewCenterY - pathBounds.centerY());
    }

    public void computeBounds(Path path) {
        path.computeBounds(pathBounds, true);
    }

    public void scaleToViewBounds(Path path) {
        scaleToViewBounds(path, SCALE_DEFAULT);
    }

    public void scaleToViewBounds(Path path, float scale) {
        scaleMatrix.reset();
        computeBounds(path);
        scale = getScale(pathBounds) * scale;
        scaleMatrix.setScale(scale, scale);
        path.transform(scaleMatrix);
    }
}
